package core.manager
{
import core.model.GameModel;
import core.model.PlayerModel;

import flash.errors.IllegalOperationError;

import values.common.BonusValue;
import values.item.IItemValue;
import values.item.INumbericItemValue;

/**
 * BonusManager
 * @author jliang
 *
 */
public class BonusManager
{
	private static function get playerModel():PlayerModel
	{
		return GameModel.instance.playerModel;
	}
	
	public static function hasEoughSpaceToAddBonus(bonus:BonusValue):Boolean
	{
		var list:Vector.<IItemValue> = bonus.bonus;
		return hasEoughSpaceToAddItemBonus(list);
	}
	
	public static function hasEoughSpaceToAddItemBonus(list:Vector.<IItemValue>):Boolean
	{
		for each (var item:IItemValue in list)
		{
			if (!playerModel.hasEnoughSpace(item)) return false;
		}
		return true;
	}
	
	public static function addItemBonus(list:Vector.<IItemValue>):void
	{
		for each (var item:IItemValue in list)
		{
			if (!playerModel.hasEnoughSpace(item)) 
			{
				throw new IllegalOperationError("There is no space to put item [" + item.key + "] into inventory");
			}
			
			playerModel.addItem(item);
			
			var num:Number = 1;
			if (item is INumbericItemValue) num = INumbericItemValue(item).num;
			
			var str:String = "增加item: " + item.key + "  x " + num;
			if (item is INumbericItemValue) str += "       剩余: " + playerModel.getItemNum(item.key);
			trace(str);
		}
	}
	
	public static function addBonus(value:BonusValue):Vector.<IItemValue>
	{
		var list:Vector.<IItemValue> = value.bonus;
		addItemBonus(list);
		return list;
	}
}
}



